from mydefine import *

def pullSC(head_recved, db):
    category = getModelCategory(head_recved['category'])
    status, rows = db.sqlexe('select name from tb_sc where category = "%s"' % (category))
    models = {"titles": ['name'], "values": [name for (name) in rows]}
    print(models)
    head_tosend = {"type": RESPONSE_PULLSC, "status":status, "models": models}
    return head_tosend


def pushSC(head_recved, db):
    category = getModelCategory(head_recved['category'])
    values = head_recved['values']
    # 查询已有分类
    status, pre_scs = db.sqlexe('select name from tb_sc where category = "%s"' % (category))
    if (None != status):
        return {"type": RESPONSE_ERROR, "status": status}
    pre_set = set(t[0] for t in pre_scs)
    cur_scs = [value['name'] for value in values]
    cur_set = set(cur_scs)
    # 增加的部分
    added_scs = cur_set.difference(pre_set)
    # 删除的部分
    removed_scs = pre_set.difference(cur_set)
    
    if len(added_scs) != 0 and len(removed_scs) == 0:           # 只增加了一项
        l_added = [[t] for t in added_scs]
        status, _ = db.sqlexe(f'insert into tb_sc (name,category) VALUES (?,"{category}")', l_added)
        if (None != status):
            return {"type": RESPONSE_ERROR, "status": status}
    elif len(added_scs) == 0 and len(removed_scs) != 0:         # 只删除了一项
        l_removed = [[t] for t in removed_scs]
        status, _ = db.sqlexe(f'delete from tb_sc where category="{category}" and name=?', l_removed)
        if (None != status):
            return {"type": RESPONSE_ERROR, "status": status}
        status, _ = db.sqlexe(f'update tb_model set description="" where description=? and category="{category}"', l_removed)
        if (None != status):
            return {"type": RESPONSE_ERROR, "status": status}
    elif len(added_scs) == 1 and len(removed_scs) == 1:  # 增加等于删除等于1，分类更改了
        addedOne = list(added_scs)[0]
        removedOne = list(removed_scs)[0]
        l_modify = [[addedOne, removedOne]]
        status, _ = db.sqlexe(f'update tb_sc set name=? where category="{category}" and name=?', l_modify)
        if (None != status):
            return {"type": RESPONSE_ERROR, "status": status}
        status, _ = db.sqlexe(f'update tb_model set description=? where category="{category}" and description=?', l_modify)
        if (None != status):
            return {"type": RESPONSE_ERROR, "status": status}
    else:                                                       # 按理说不可能出现的情况
        return {"type": RESPONSE_ERROR, "status": f'增数 = {len(added_scs)} , 删数 = {len(removed_scs)} , 为未定义行为.'}
    
    head_tosend = {"type": RESPONSE_PUSHSC, "status": status, "models": {}}
    return head_tosend


def standardClassification(head_recved, db):
    block_size = head_recved['block_size']

    # if (0 == len(block_size)):
    #     return {"type": RESPONSE_ERROR, "status": "没有数据！"}

    scType = head_recved['scType']

    if REQUEST_PULLSC == scType:
        head_tosend = pullSC(head_recved, db)
    if REQUEST_PUSHSC == scType:
        head_tosend = pushSC(head_recved, db)

    return head_tosend
